package com.iptv.grape.jss;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.iptv.grape.R;
//import com.iptv.grape.bu.AppConfig;
//import com.iptv.grape.bu.GrapeContext;
//import com.iptv.grape.bu.imp.QuitTimeLiveMode;
//import com.iptv.grape.domain.Channel;
import com.iptv.grape.ui.BaseFrgmt;
//import com.iptv.grape.ui.frgmt.TimeLiveFragment;
import com.iptv.grape.util.CommUtil;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class WebFrgmt extends BaseFrgmt implements View.OnFocusChangeListener,
        SeekBar.OnSeekBarChangeListener{
    // Activity
    private SeekBar seekbar;
    private TextView time_left;
    private TextView time_right;
    private TextView timing;
    private int curTimeArga = -30;

    // 时移总长度
    private int maxVal;
    private int stVal = 60;

    public void setMaxVal(int maxVal){
        this.maxVal = maxVal;
        // 时移时长 2 个小时
        seekbar.setMax(maxVal);
        //xMediaPlayerEventListener = mActivity.getDefXMediaPlayerEventListener();
        //xMediaPlayerEventListener.addProgressListener(this);
        //xMediaPlayerEventListener.addPlayCompletionListener(this);
        //GrapeContext.curGrapeContext().getCurMediaPlayer().setSeekbar(seekbar);
        //GrapeContext.curGrapeContext().getCurMediaPlayer().setOnPlayerListener(this);
        // 单次进度一次30秒
        seekbar.setKeyProgressIncrement(stVal);
        timing.setText(CommUtil.formatDate(CommUtil.addData(-stVal),"HH:mm"));
        seekbar.requestFocus();
        curTimeArga = -30;
        seekbar.setProgress(maxVal - stVal);
    }

    // 切换URL Handler
    private Handler changeUrlHandler = new Handler();

    private Timer timer = new Timer();
    TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            WebFrgmt.this.mActivity.runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    Date date = CommUtil.addData(-maxVal);
                    time_left.setText(CommUtil.formatDate(date,"HH:mm"));
                    timing.setText(CommUtil.formatDate(CommUtil.addData(-subVal),"HH:mm"));
                }
            });

        }
    };


    @Override
    protected int layoutId() {
        return R.layout.vod_fragment;
    }

    @Override
    protected void initView(@NonNull View root, @Nullable Bundle savedInstanceState) {
        maxVal = getArguments().getInt("maxTime");
        time_left = root.findViewById(R.id.time_left);
        time_right = root.findViewById(R.id.time_right);
        timing = root.findViewById(R.id.timing);
        seekbar = root.findViewById(R.id.seekbar);
        seekbar.setTag("seekbar");
        seekbar.setOnSeekBarChangeListener(this);
        seekbar.setOnFocusChangeListener(this);
        this.time_right.setText("现在");

        Date date = CommUtil.addData(-maxVal);
        this.time_left.setText(CommUtil.formatDate(date,"HH:mm"));

        // 时移时长 2 个小时
        seekbar.setMax(maxVal);
        //xMediaPlayerEventListener = mActivity.getDefXMediaPlayerEventListener();
        //xMediaPlayerEventListener.addProgressListener(this);
        //xMediaPlayerEventListener.addPlayCompletionListener(this);
        //GrapeContext.curGrapeContext().getCurMediaPlayer().setSeekbar(seekbar);
        //GrapeContext.curGrapeContext().getCurMediaPlayer().setOnPlayerListener(this);
        // 单次进度一次30秒
        seekbar.setKeyProgressIncrement(stVal);
        timing.setText(CommUtil.formatDate(CommUtil.addData(-stVal),"HH:mm"));
        seekbar.requestFocus();
        curTimeArga = -30;
        seekbar.setProgress(maxVal - stVal);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                int quotaWidth = timing.getWidth();
                final Drawable thumb = seekbar.getThumb();
                //获取thumb的位置
                final Rect bounds = thumb.getBounds();
                timing.setX((thumb.getIntrinsicWidth() - quotaWidth) / 2
                        + bounds.left + seekbar.getX());

            }
        }, 200);

        timer.schedule(timerTask,1000,1000 * 30);
        seekbar.requestFocus();
//        curChannel.playTimeMove(curTimeArga);
    }

    // 销毁
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();

    }

    // 界面隐藏显示回调函数
    @Override
    public void onHiddenChanged(boolean hidden){
        super.onHiddenChanged(hidden);
        // 不在最前端界面显示
        if (hidden) {
            // 隐藏
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //rel_horitation.reSetFocus();
                    seekbar.requestFocus();

                }
            }, 100);
        }
    }

    public static WebFrgmt newInstance() {
        WebFrgmt frag = new WebFrgmt();
        Bundle bundle = new Bundle();
        frag.setArguments(bundle);   //设置参数
        return frag;
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(hasFocus){
            timing.setBackgroundResource(R.drawable.timing_fucs);
            timing.setTextColor(this.mActivity.getResources().getColor(R.color.white));
        }else {
            timing.setBackgroundResource(R.drawable.timing_blus);
            timing.setTextColor(this.mActivity.getResources().getColor(R.color.item_bill4));
        }
    }

//    @Override
//    public void onProgress(int speed) {
//        //AppLog.getCurLog().out("VodFragment -------- onProgress" + speed);
//        seekbar.setProgress(speed);
//    }

    private int subVal;
    @Override
    public synchronized void onProgressChanged(SeekBar seekbar, int progressVal, boolean isUserSet) {
        if(isUserSet) {
            //XMediaPlayer mediaPlayer = GrapeContext.curGrapeContext().getCurMediaPlayer();
            //if(!mediaPlayer.isPlaying()){
            //   mediaPlayer.start();
            // }
            //mediaPlayer.seekTo(progressVal);
        }
        int quotaWidth = timing.getWidth();
        final Drawable thumb = seekbar.getThumb();
        //获取thumb的位置
        final Rect bounds = thumb.getBounds();

        //AppLog.getCurLog().out("onProgressChanged = " + progressVal);
        //curStartDate.setTime(curStartDate.getTime() + i);
        //timing.setText(CommUtil.getShowTime(progressVal));
        timing.setX((thumb.getIntrinsicWidth() - quotaWidth) / 2
                + bounds.left + seekbar.getX());

        // 计算时差
        subVal = maxVal - progressVal;

        if(subVal == 0){
            return;
        }
        timing.setText(CommUtil.formatDate(CommUtil.addData(-subVal),"HH:mm"));

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}

